from selenium import webdriver
from selenium.common.exceptions import InvalidSessionIdException, WebDriverException, NoSuchFrameException
from selenium.webdriver.common.by import By
import time

from selenium.webdriver.support.select import Select

from common.functions import open_browser
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class Base:
    def __init__(self, browser):
        self.driver = browser
        self.driver.maximize_window()
# 网址
    def get(self, url):
        try:
            self.driver.get(url)
        except (InvalidSessionIdException, WebDriverException):
            print('输入的网址不正确')
# 定位
    def find_ele(self, locator,tomeout=2):
        try:
            return WebDriverWait(self.driver,timeout=tomeout).until(EC.presence_of_element_located(locator))
        except NoSuchFrameException:
            print('元素定位失败')
# 输入
    def send_keys(self, locator, value):
        send = self.find_ele(locator)
        if send is not None:
            send.send_keys(value)
        else:
            print('元素输入错误')
        time.sleep(2)
# 清空
    def clear(self, locator):
        cl = self.find_ele(locator)
        if cl is not None:
            cl.clear()
        else:
            print('清除错误')
        time.sleep(2)
# 浏览器后退
    def back(self):
        self.driver.back()
# 后退
    def forward(self):
        self.driver.forward()
# 刷新
    def refresh(self):
        self.driver.refresh()
# 获取元素
    def get_attribute(self, locator, value):
        get = self.find_ele(locator)
        if get is not None:
            get.get_attribute(value)
        else:
            print('获取元素错误')
# 获取文本
    def get_text(self, locator):
        gt = self.find_ele(locator)
        if gt is not None:
            return gt.text
        else:
            print('获取文本错误')
# 单击
    def click(self, locator):
        element = self.find_ele(locator)
        if element is not None:
            element.click()
        else:
            print('点击失败')
# 关闭网页
    def quit(self, seconds=2):
        time.sleep(seconds)
        self.driver.quit()

#select下拉的三种方式
    #索引
    def select_by_index(self,locator,i):
        so = self.find_ele(locator)
        self.select = Select(so)
        if so is not None:
            self.select.select_by_index(i)
        else:
            print('没有找到下拉框')
    #value
    def select_by_value(self,locator,value):
        so = self.find_ele(locator)
        self.select = Select(so)
        if so is not None:
            self.select.select_by_value(value)
        else:
            print('没有找到下拉框')
    #文本
    def select_by_visible_text(self,locator,text):
        so = self.find_ele(locator)
        self.select = Select(so)
        if so is not None:
            self.select.select_by_visible_text(text)
        else:
            print('没有找到下拉框')

#滚动
    #滚动到最下方
    def scroll_low(self):
        time.sleep(2)
        js1 = 'window.scrollTo(0,document.body.scrollHeight)'
        return self.driver.execute_script(js1)
   #滚动到最上方
    def scroll_top(self):
        time.sleep(2)
        js2 = 'window.scrollTo(0,0)'
        return self.driver.execute_script(js2)
    #滚动到聚焦位置
    def scroll_target(self,locator):
        time.sleep(2)
        target = self.find_ele(locator)
        return self.driver.execute_script('arguments[0].scrollIntoView();', target)

#切换iframe
    def swtich_iframe(self, locator):  # 切换到指定的iframe
        frame = self.find_ele(locator)
        self.driver.switch_to.frame(frame)


    def switch_to_default_content(self):  # 切出frame
        self.driver.switch_to.default_content()

# 弹窗
    def alert_accpet(self):
        alert=self.driver.switch_to.alert
        alert.accept()





if __name__ == '__main__':
    driver = open_browser()
    base = Base(driver)  # todo  实例化
    base.get('https://www.baidu.com')
    base.find_ele((By.ID, 'kw'))
    base.get_text((By.LINK_TEXT,'新闻'))
    base.send_keys((By.ID, 'kw'), '123456')
    base.clear((By.ID, 'kw'))
    time.sleep(2)
    base.send_keys((By.ID, 'kw'), '123456')
    base.click((By.ID, 'su'))
    base.get_attribute((By.ID,'kw'),'outerHTML')
    base.back()
    base.forward()
    base.refresh()
    base.quit()
